~<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>手写instanceOf</title>
	</head>
	<body>
		<script>
			function myInstanceof(target, origin) {
				if (typeof target !== 'object' || target === null) return false
				if (typeof origin !== 'function')
					throw new TypeError('origin must be function')
				let proto = Object.getPrototypeOf(target) // 相当于 proto = target.__proto__;
				while (proto) {
					if (proto === origin.prototype) return true
					proto = Object.getPrototypeOf(proto)
				}
				return false
			}

			// 测试
			function Car(make, model, year) {
				this.make = make
				this.model = model
				this.year = year
			}
			const auto = new Car('Honda', 'Accord', 1998)
			console.log(myInstanceof(auto, Car)) // true
			console.log(myInstanceof(auto, Object)) // true
		</script>
	</body>
</html>
